﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Crolengi, this program (a part of program) is gameengine based on C++\Directx
//    Copyright (C) 2008 Pashinin Anton Alekseevich (Crol)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//    (INCLUDING NEGLIGENCE OR OTHERWISE) 
//    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//    Contacts: 
//    Mail: crolengi@gmail.com
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#ifndef  TextBox_h
#define  TextBox_h



#include "include\\Utils\\DrawingFunctions.h"
#include "include\\Utils\\defines01.h"

//#################################################################################################################
///// 
/////   :: РљР›РђРЎРЎ ::
///// 
//#################################################################################################################
class CLREngi_UI_TextBox
{
	protected:

	struct CUSTOMVERTEX
	{
		float x, y, z,w;
		D3DCOLOR color;
	};

	#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE )

  protected:
	LPDIRECT3DDEVICE9       *Device;
	D3DCOLOR                 BackColor,TextColor,BgColor;
	LPDIRECT3DVERTEXBUFFER9  *g_pVB;
	LPDIRECT3DVERTEXBUFFER9  *g_pVB2;
	LPDIRECT3DVERTEXBUFFER9  *g_pVB3;
	LPDIRECT3DTEXTURE9       *g_pTexture ;
	LPD3DXFONT               *Font; 
	LPD3DXFONT               *FontName; 

	int                      pos_x,
		                     pos_y,
							 width,
							 height;
	int                      shifter_x,
		                     shifter_y;

	int                      hAdd;

	bool                     selected;	
	bool                     allow_shift;
	bool                     allow_text_shift;

	char                     strbuffer[1024];
	CRLEngi_string           CommandBuffer;
	CRLEngi_string           Name;


	unsigned short limit_b,limit_a;
	
	POINT       last_mouse_point;
	POINT       curr_mouse_point;
	POINT       last_mouse_pitch;


	public:
	////////////////////////////////////////
	DynamicRow                 *TextLine;
	Timer                       InputTimer;
	///////////////
    protected:
	int LOGid;
	int lines;
	int id;
	
public:
//#################################################################################################################
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int           DrawText(CRLEngi_string text,int pos_x,int pos_y,D3DCOLOR color){
		RECT destRect;
		SetRect( &destRect, pos_x, pos_y,pos_x+ width-10, pos_y+height-20 ); 
		(*Font)->DrawText( NULL,text.getChars(), -1, &destRect, DT_NOCLIP , color );
		//return (strlen(text.getChars())/((width-10)/6))*14+14;
		return 0;
		
}//void
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Render(){

	       
	            //(*Device)->SetTexture( 0, NULL );

				(*Device)->SetStreamSource( 0, *g_pVB, 0, sizeof(CUSTOMVERTEX) );
				(*Device)->SetFVF( D3DFVF_CUSTOMVERTEX );
				(*Device)->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

				(*Device)->SetStreamSource( 0, *g_pVB2, 0, sizeof(CUSTOMVERTEX) );
				(*Device)->SetFVF( D3DFVF_CUSTOMVERTEX );
				(*Device)->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

				(*Device)->SetStreamSource( 0, *g_pVB3, 0, sizeof(CUSTOMVERTEX) );
				(*Device)->SetFVF( D3DFVF_CUSTOMVERTEX );
				(*Device)->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

				RECT destRect;
				SetRect( &destRect, pos_x, pos_y-12,0,0);
				if (FontName!=NULL)
		        (*FontName)->DrawText( NULL, Name.getChars(), -1, &destRect, DT_NOCLIP,  TextColor );

				//(*Device)->SetTexture( 0, NULL );	

				if ((!allow_shift)&&(selected)){
				drawLine(D3DXVECTOR3(pos_x+width-16, pos_y-1, 0.0f), D3DXVECTOR3(pos_x+width-16, pos_y+height+1, 0.0f),Device);
				drawLine(D3DXVECTOR3(pos_x+width-19, pos_y-1, 0.0f), D3DXVECTOR3(pos_x+width-19, pos_y+height+1, 0.0f),Device);
				drawLine(D3DXVECTOR3(pos_x-1,      pos_y-1        ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y-1,         0.0f),Device);
				drawLine(D3DXVECTOR3(pos_x-1,      pos_y+height+1 ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y+height+1,  0.0f),Device);
				drawLine(D3DXVECTOR3(pos_x-1,       pos_y-1, 0.0f), D3DXVECTOR3(pos_x-1,       pos_y+height+1, 0.0f),Device);
				drawLine(D3DXVECTOR3(pos_x+width+1, pos_y-1, 0.0f), D3DXVECTOR3(pos_x+width+1, pos_y+height+1, 0.0f),Device);

				
				}
				else
				if ((selected)&&(allow_shift)){
				drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y+1       -last_mouse_pitch.y ,  0.0f),Device);
				drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y-height+1 -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
				drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x+1       +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
				drawLine(D3DXVECTOR3(curr_mouse_point.x-width-1+last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
				}
								

				if (allow_text_shift){
					if ((curr_mouse_point.y>pos_y+height-6)||((curr_mouse_point.y<pos_y+6)))
						allow_text_shift=false;
				   else{

				   drawLine(D3DXVECTOR3(pos_x+width-13,curr_mouse_point.y-6 ,0.0f),D3DXVECTOR3(pos_x+width-3,curr_mouse_point.y-6 ,0.0f),Device);
				   drawLine(D3DXVECTOR3(pos_x+width-13,curr_mouse_point.y+6 ,0.0f),D3DXVECTOR3(pos_x+width-3,curr_mouse_point.y+6 ,0.0f),Device);
				   drawLine(D3DXVECTOR3(pos_x+width-3 ,curr_mouse_point.y-6 ,0.0f),D3DXVECTOR3(pos_x+width-3,curr_mouse_point.y+6 ,0.0f),Device);
				   drawLine(D3DXVECTOR3(pos_x+width-13,curr_mouse_point.y-6 ,0.0f),D3DXVECTOR3(pos_x+width-13,curr_mouse_point.y+6 ,0.0f),Device);
				   }
				}
				//(*Device)->SetTexture( 0, NULL );	
					

			     hAdd = 0;
				 LOGid = 1;
				 lines = TextLine->iCountKnots(LOGid);
				 id = (*TextLine).TopId(LOGid);
				for (int i=1;i<=lines;i++){
					  if ((i <= limit_b)&&(i >= limit_a )){
						 if ((*TextLine).iLookName((*TextLine).TopId(id)) == "_TEXT_")
					       hAdd +=  DrawText((*TextLine).iLookName(id),pos_x+12,   hAdd+pos_y+(i-limit_a)*10,   D3DCOLOR_XRGB(235,235,235));
						 else
						 if ((*TextLine).iLookName((*TextLine).TopId(id)) == "_ERROR_")
						   hAdd +=  DrawText((*TextLine).iLookName(id),pos_x+12,   hAdd+pos_y+(i-limit_a)*10,   D3DCOLOR_XRGB(237,83,52));
						 else
						 if ((*TextLine).iLookName((*TextLine).TopId(id)) == "_COMMENT_")
						   hAdd +=  DrawText((*TextLine).iLookName(id),pos_x+12,   hAdd+pos_y+(i-limit_a)*10,   D3DCOLOR_XRGB(254,242,84));
						 else
						 if ((*TextLine).iLookName((*TextLine).TopId(id)) == "_SYSTEM_")
						   hAdd +=  DrawText((*TextLine).iLookName(id),pos_x+12,   hAdd+pos_y+(i-limit_a)*10,   D3DCOLOR_XRGB(20,96,182));
					    }
					id = (*TextLine).NextId(id);
				    }


}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Setup(int x,int y,int w,int h,D3DCOLOR    BColor){
	BackColor=BColor;
	pos_x =x;
	pos_y =y;
	width =w;
	height=h;
	shifter_x = pos_x+width-8;
	shifter_y = pos_y+8;
	int lines = (*TextLine).iCountKnots(1);
	int can =height/10-3;

	limit_b=can;
	limit_a=0;

	Init();

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool           MouseEvent(unsigned short event_index,POINT m){
	curr_mouse_point = m;

	if (event_index==1)
	{
		BgColor = D3DCOLOR_XRGB(70,70,70);
		
	if ((m.x>pos_x)&&(m.x<pos_x+width)&&(m.y>pos_y)&&(m.y<(pos_y+height))){
		selected = true;
		TextColor = D3DCOLOR_XRGB(255,255,255);
		BgColor = D3DCOLOR_XRGB(70,70,70);
	
		last_mouse_point = m;
		last_mouse_pitch.x = (pos_x+width  -m.x);
		last_mouse_pitch.y = -(pos_y+height -m.y);
		
		if ((m.x>pos_x+width-12)&&(m.x<(pos_x+width-2))&&(m.y<(shifter_y+12)&&(m.y>(shifter_y-12))))
		  allow_text_shift = true;
		else
		  allow_shift = true;

		return true;
	    }

	selected = false;
	
	
	TextColor = D3DCOLOR_XRGB(200,200,200);
	return false;
	}
	else
	if ((event_index==2)&&(allow_shift)&&(selected)){
	  BgColor = D3DCOLOR_XRGB(70,70,70);
	  pos_x +=m.x-last_mouse_point.x;
	  pos_y +=m.y-last_mouse_point.y;
	  shifter_x +=m.x-last_mouse_point.x;
	  shifter_y +=m.y-last_mouse_point.y;
	 // shifter_x = pos_x+width-8;	
	 // shifter_y = pos_y+8;

	  allow_shift = false;
	  Init();		
	}else // Р·РЅР°С‡РёС‚ РїРµСЂРµС‚Р°СЃРєРёРІР°РµРј?
	if ( (((event_index==2)&&(allow_text_shift)&&(selected))&&
	      (last_mouse_point.y>shifter_y-6)&&(last_mouse_point.y<shifter_y+6))&&
		  (m.y>pos_y+5)&&(m.y<(pos_y+height-5))){

		int shift = (last_mouse_point.y-m.y);
		shifter_y=m.y-6;

		float lines  = (*TextLine).iCountKnots(1)+hAdd;///14;
		float limAdd;
		float px;
		//РїРѕСЃС‡РёС‚Р°С‚СЊ РІС‹СЃРѕС‚Р° РїРѕР»РѕСЃС‹/С‡РёСЃР»Рѕ СЃС‚СЂРѕРє
		px  = height/(lines+10);
		//РїРѕСЃС‡РёС‚Р°С‚СЊ С‡РёСЃР»Рѕ СЃС‚СЂРѕРє РґР»СЏ С‚РµРєСѓС‰РµРіРѕ РїРѕР»РѕР¶РµРЅРёСЏ Р±РµРіСѓРЅРєР°
		limAdd = (shifter_y-pos_y)/px;
		
        limit_a = limAdd; 
		limit_b =  height/(10)-2 + limAdd;

		last_mouse_point = m;
		allow_text_shift = false;
		Init();
			
	}

	//РґР»СЏ РјРёРіР°РЅРёСЏ
	else if (!selected){
	if ((m.x>pos_x)&&(m.x<pos_x+width)&&(m.y>pos_y)&&(m.y<(pos_y+height))&&(!allow_shift)){
	  BgColor = D3DCOLOR_XRGB(70,70,70);
	  Init();
	}else{BgColor = D3DCOLOR_XRGB(60,60,60);
	  Init();
	}}
	else if (selected)BgColor = D3DCOLOR_XRGB(70,70,70) ;

	return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Select(){	selected = true;	TextColor = D3DCOLOR_XRGB(255,255,255);}
void           Deselect(){selected = false;   TextColor = D3DCOLOR_XRGB(200,200,240);}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT        Init(){
	
	SAFE_RELEASE(g_pVB);
	g_pVB = new LPDIRECT3DVERTEXBUFFER9();
	SAFE_RELEASE(g_pVB2);
	g_pVB2 = new LPDIRECT3DVERTEXBUFFER9();
	SAFE_RELEASE(g_pVB3);
	g_pVB3 = new LPDIRECT3DVERTEXBUFFER9();
	SAFE_RELEASE(g_pTexture);
	g_pTexture = new LPDIRECT3DTEXTURE9();
	SAFE_RELEASE(Font);
	Font = new LPD3DXFONT();
		    
	bool a = true;
	a = D3DXCreateTextureFromFile( *Device, "textures\\_def\\white.bmp", g_pTexture );

   CUSTOMVERTEX vertices[] =
    {
		{ pos_x        , pos_y+height     , 0.2f, 1.0f, BgColor  }, // 
        { pos_x        , pos_y             , 0.2f, 0.0f, BgColor	 }, // //*
        { pos_x+width-20  , pos_y+height   , 0.2f, 1.0f, BgColor	 },    //*
		{ pos_x+width-20  , pos_y          , 0.2f, 0.0f, BgColor	 },
    };

    if( FAILED( (*Device)->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

	VOID* pVertices;
    if( FAILED( (*g_pVB)->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, vertices, sizeof(vertices) );
	(*g_pVB)->Unlock();



	CUSTOMVERTEX vertices1[] =
    {
		{ pos_x  -4      , pos_y+height +4  , 0.3f, 1.0f, D3DCOLOR_XRGB(47,47,47)  }, // 
        { pos_x  -4      , pos_y       -12  , 0.3f, 0.0f, D3DCOLOR_XRGB(47,47,47) 	 }, // //*
        { pos_x+width +4 , pos_y+height +4  , 0.3f, 1.0f, D3DCOLOR_XRGB(47,47,47) 	 },    //*
		{ pos_x+width +4 , pos_y       -12  , 0.3f, 0.0f, D3DCOLOR_XRGB(47,47,47)	 },
    };

    if( FAILED( (*Device)->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, g_pVB3, NULL ) ) )
    {
        return E_FAIL;
    }

	VOID* pVertices1;
    if( FAILED( (*g_pVB3)->Lock( 0, sizeof(vertices1), (void**)&pVertices1, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices1, vertices1, sizeof(vertices1) );
	(*g_pVB3)->Unlock();


    CUSTOMVERTEX vertices2[] =
    {
		{ shifter_x-6  ,shifter_y+6   , 0.01f, 1.0f,  D3DCOLOR_XRGB(255,255,255)  }, // 
        { shifter_x-6  ,shifter_y-6   , 0.01f, 0.0f,  D3DCOLOR_XRGB(255,255,255)  }, // //*
        { shifter_x+6  ,shifter_y+6   , 0.01f, 1.0f,  D3DCOLOR_XRGB(255,255,255)  },    //*
		{ shifter_x+6  ,shifter_y-6   , 0.01f, 0.0f,  D3DCOLOR_XRGB(255,255,255)  },
    };

    if( FAILED( (*Device)->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, g_pVB2, NULL ) ) )
    {
        return E_FAIL;
    }

	VOID* pVertices2;
    if( FAILED( (*g_pVB2)->Lock( 0, sizeof(vertices2), (void**)&pVertices2, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices2, vertices2, sizeof(vertices2) );
	(*g_pVB2)->Unlock();



	D3DXCreateFont( (*Device), 14,6, FW_ULTRALIGHT, 0, FALSE, 
		                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		                 DEFAULT_PITCH | FF_DONTCARE, TEXT("Courier "), 
		                 Font );

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//#################################################################################################################
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                     РљРћРќРЎРўР РЈРљРўРћР 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CLREngi_UI_TextBox(LPDIRECT3DDEVICE9 *D,DynamicRow *Text,char *nm){
   pos_x  = 0;
   pos_y  = 0;
   width  = 5;
   height = 15;
   Device = D;
   g_pVB=NULL;
   g_pVB2=NULL;
   g_pVB3=NULL;
   g_pTexture=NULL ;
   Font=NULL; 
   selected = false;
   TextColor = D3DCOLOR_XRGB(145,145,155);
   CommandBuffer = "_";
   TextLine = Text;  
   allow_shift = false;
   Name = nm;
   BgColor = D3DCOLOR_XRGB(81,81,81);
   BackColor = D3DCOLOR_XRGB(150,150,200);
   allow_text_shift=false;
   shifter_x = height - 12;
   shifter_y = pos_y +5;

   FontName = new LPD3DXFONT();
   D3DXCreateFont( (*Device), 10,5, FW_ULTRALIGHT, 0, FALSE, 
		                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		                 DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), 
		                 FontName );
   limit_b=25;limit_a=1;
   }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                       Р”Р•РЎРўР РЈРљРўРћР 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~CLREngi_UI_TextBox(void)
   {
	   SAFE_RELEASE(g_pVB);
	   SAFE_RELEASE(g_pTexture);
	   SAFE_RELEASE(Font);
	   SAFE_RELEASE(FontName);
	   SAFE_RELEASE(g_pVB2);	
	   SAFE_RELEASE(g_pVB3);
   }

};

#endif 